#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/12/6 16:01
# @Author  : Beam
# @Site    : 
# @File    : getHardware.py
# @Software: PyCharm

# global HARDWARE
# HARDWARE = None

class SaltClient(object):
	def __init__(self):
		global HARDWARE
		self.HARDWARE = None
		from script import saltcollect
		self.salt = saltcollect.SaltApi()

	def combine(self,data):
		'''用于组合字典'''
		# global HARDWARE
		if not self.HARDWARE:
			self.HARDWARE = data
		else:
			for k,v in self.HARDWARE.items():
				for j,o in data.items():
					if k == j:
						self.HARDWARE[k] = dict(v,**o)

	def getminion_id(self,hostname):
		minion_id = self.salt.getresult(hostname, 'grains.item', 'id')
		return minion_id

	def getlocalhost(self,hostname):
		localhost = self.salt.getresult(hostname, 'grains.item', 'localhost')
		return localhost

	def getmanufacturer(self,hostname):
		manufacturer = self.salt.getresult(hostname, 'grains.item', 'manufacturer')
		return manufacturer

	def getcpu_model(self,hostname):
		cpu_model = self.salt.getresult(hostname, 'grains.item', 'cpu_model')
		return cpu_model

	def getcpuarch(self,hostname):
		cpuarch = self.salt.getresult(hostname, 'grains.item', 'cpuarch')
		return cpuarch

	def getkernel(self,hostname):
		kernel = self.salt.getresult(hostname, 'grains.item', 'kernel')
		return kernel

	def getkernelrelease(self,hostname):
		kernelrelease = self.salt.getresult(hostname, 'grains.item', 'kernelrelease')
		return kernelrelease

	def getmachine_id(self,hostname):
		machine_id = self.salt.getresult(hostname, 'grains.item', 'machine_id')
		return machine_id

	def getmem_total(self,hostname):
		mem_total = self.salt.getresult(hostname, 'grains.item', 'mem_total')
		return mem_total

	def getnum_cpus(self,hostname):
		num_cpus = self.salt.getresult(hostname, 'grains.item', 'num_cpus')
		return num_cpus

	def getosfullname(self,hostname):
		osfullname = self.salt.getresult(hostname, 'grains.item', 'osfullname')
		return osfullname

	def getproductname(self,hostname):
		productname = self.salt.getresult(hostname, 'grains.item', 'productname')
		return productname

	def getserialnumber(self,hostname):
		serialnumber = self.salt.getresult(hostname, 'grains.item', 'serialnumber')
		return serialnumber

	def getnetwork(self,hostname):
		networkinfo = self.salt.getresult(hostname, 'grains.item', 'ipv4')
		return networkinfo

	def getdisk(self,hostname):
		result = {}
		con = {}
		diskinfo = self.salt.getresult(hostname,'disk.usage',None)
		for k,v in diskinfo.items():
			if not k in result:
				result[k] = None
			for x,y in v.items():
				for a,b in y.items():
					if a == 'filesystem':
						# if not x in con:
						# 	con[x] = None
						if len(b.split('/')) >= 2:
							con[x] = y
			result[k] = con
		return result

	def gethardware(self,hostname):
		minion_id = self.getminion_id(hostname)
		self.combine(minion_id)
		localhost = self.getlocalhost(hostname)
		self.combine(localhost)
		manufacturer = self.getmanufacturer(hostname)
		self.combine(manufacturer)
		cpu_model = self.getcpu_model(hostname)
		self.combine(cpu_model)
		cpuarch = self.getcpuarch(hostname)
		self.combine(cpuarch)
		kernel = self.getkernel(hostname)
		self.combine(kernel)
		kernelrelease = self.getkernelrelease(hostname)
		self.combine(kernelrelease)
		machine_id = self.getmachine_id(hostname)
		self.combine(machine_id)
		mem_total = self.getmem_total(hostname)
		self.combine(mem_total)
		num_cpus = self.getnum_cpus(hostname)
		self.combine(num_cpus)
		osfullname = self.getosfullname(hostname)
		self.combine(osfullname)
		productname = self.getproductname(hostname)
		self.combine(productname)
		serialnumber = self.getserialnumber(hostname)
		self.combine(serialnumber)
		return self.HARDWARE

	def getnetworkinfo(self,hostname):
		neworkinfo = self.getnetwork(hostname)
		for k,v in neworkinfo.items():
			for i,j in v.items():
				for x in j:
					if x == '0.0.0.0' or x == '127.0.0.1':
						j.remove(x)
		return neworkinfo

	def getdiskinfo(self,hostname):
		diskinfo = self.getdisk(hostname)
		return diskinfo


#
#
# salt = SaltClient()
# HARDWARE = salt.getinfo('*')
# for k,v in HARDWARE.items():
# 	print('Key:'+k)
# 	print('Value:'+str(v))


